﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_navigation_assign : System.Web.UI.Page
    {
        private Guid _selectedApplicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _selectedApplicationId = new Guid(Request.QueryString["id"]);

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                List<Role> roles = service.GetRoles(0, 999);
                _ctlListRole.DataSource = roles;
                _ctlListRole.DataBind();
                _ctlListRole.SelectedIndex = 0;
                _ctlListRole_SelectedIndexChanged(null, null);
            }
        }

        #region 导航设置
        protected void _ctlListRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_ctlListRole.SelectedValue))
            {
                //Guid selectedRoleId = new Guid(_ctlListRole.SelectedValue);
                //ICoreService service = ServiceLocator.Get<ICoreService>();
                //List<Navigation> allNavigations = service.GetNavigations(_selectedApplicationId);
                //List<Navigation> roleNavigations = service.GetNavigations(_selectedApplicationId, selectedRoleId);

                //_ctlTreeNavigation.Nodes.Clear();
                //foreach (var item in allNavigations)
                //{
                //    Convert(_ctlTreeNavigation, item, roleNavigations);
                //}
                //_ctlTreeNavigation.ExpandAll();
            }
        }
        private void Convert(TreeView tree, Navigation navigation, List<Navigation> roleNavigations)
        {
            TreeNode node = new TreeNode();
            node.Text = navigation.Caption;
            node.Checked = IsAssigned(navigation, roleNavigations);
            node.Value = navigation.Id.ToString("D");
            tree.Nodes.Add(node);
            foreach (Navigation subNavigation in navigation.Children)
            {
                Convert(node, subNavigation, roleNavigations);
            }
        }
        private void Convert(TreeNode parentNode, Navigation navigation, List<Navigation> roleNavigations)
        {
            TreeNode node = new TreeNode();
            node.Text = navigation.Caption;
            node.Checked = IsAssigned(navigation, roleNavigations);
            node.Value = navigation.Id.ToString("D");
            parentNode.ChildNodes.Add(node);
            foreach (Navigation subNavigation in navigation.Children)
            {
                Convert(node, subNavigation, roleNavigations);
            }
        }
        private bool IsAssigned(Navigation navigation, List<Navigation> roleNavigations)
        {
            Stack<Navigation> stack = new Stack<Navigation>();
            foreach (var item in roleNavigations)
            {
                stack.Push(item);
            }

            while (stack.Count > 0)
            {
                Navigation popItem = stack.Pop();
                if (popItem.Id == navigation.Id)
                {
                    return true;
                }

                foreach (var subItem in popItem.Children)
                {
                    stack.Push(subItem);
                }
            }
            return false;
        }

        protected void _ctlBtnSetNavigation_Click(object sender, EventArgs e)
        {
            List<Guid> navigationIds = new List<Guid>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            foreach (TreeNode node in _ctlTreeNavigation.Nodes)
            {
                stack.Push(node);
            }

            while (stack.Count > 0)
            {
                TreeNode popNode = stack.Pop();
                if (popNode.Checked)
                {
                    navigationIds.Add(new Guid(popNode.Value));
                }

                foreach (TreeNode item in popNode.ChildNodes)
                {
                    stack.Push(item);
                }
            }

            //ICoreService service = ServiceLocator.Get<ICoreService>();
            //Guid selectedRoleId = new Guid(_ctlListRole.SelectedValue);
            //service.SetRoleNavigations(navigationIds, selectedRoleId);
        }
        #endregion
    }
}